﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcCarConsume.Domain.Entites;
using System.Text.RegularExpressions;

namespace MvcCarConsume.Domain
{
    public class SqlCarService : ICarService
    {
        private string connStr;

        public SqlCarService(string connectionString)
        {
            connStr = connectionString;
        }

        public IList<Entites.Type> Categories
        {
            get{
                using (var db = new MvcCarDBEntities(connStr))
                {
                    return db.Type.ToList();
                }
            }
        }

        public IList<Brand> Brands
        {
            get {
                using (var db = new MvcCarDBEntities(connStr))
                {
                    return db.Brand.ToList();
                }
            }
        }

        public MvcCarConsume.Domain.Entites.Type CategoryWithID(int categoryid)
        {
            using (var db = new MvcCarDBEntities())
            {
                return db.Type.FirstOrDefault(p => p.TypeId == categoryid);
            }
        }

        public IList<Car> CarsWithCurrentCategory(string categoryName)
        {
            return CarsWithCurrentCategoryAndFilter(categoryName, null);
        }

        public Car CarWithID(int id)
        {
            using (var db = new MvcCarDBEntities())
            {
                return db.Car.FirstOrDefault(p => p.CarId == id);
            }
        }

        public IList<Car> CarsWithCurrentCategoryAndFilter(string categoryName, IList<Func<Car, bool>> filters)
        {
            if (categoryName == null)
                return new List<Car>();
            using (var db = new MvcCarDBEntities(connStr))
            {
                IList<Car> ret;
                if (categoryName == "All")
                    ret= db.Car.ToList();
                else
                    ret = db.Car.Where(p => p.Type.TypeName == categoryName).ToList();
                if (filters != null)
                {
                    foreach (var filter in filters)
                    {
                        ret = ret.Where(filter).ToList();
                    }
                }
                return ret;
            }
        }
        public IList<Car> CarsWithCurrentCategoryAndFilter(int categoryId, IList<Func<Car, bool>> filters)
        {
            using (var db = new MvcCarDBEntities(connStr))
            {
                IList<Car> ret;
                if (categoryId == 0)
                    ret = db.Car.ToList();
                else
                    ret = db.Car.Where(p => p.Type.TypeId == categoryId).ToList();
                if (filters != null)
                {
                    foreach (var filter in filters)
                    {
                        ret = ret.Where(filter).ToList();
                    }
                }
                return ret;
            }
        }

        public IEnumerable<Car> GetShowCars()
        {
            List<Car> cars = new List<Car>();
            using (var db = new MvcCarDBEntities(connStr))
            {
                var c = db.Car.Where(o => o.Count != 0);
                foreach (var car in c)
                cars.Add(new Car
                {
                    CarId = car.CarId,
                    CarName = car.CarName,
                    BrandId = car.BrandId,
                    TypeId = car.TypeId,
                    Count = car.Count,
                    Price = car.Price,
                    Description = car.Description,
                });
            }
            return cars;
        }
    }
}
